const { Router } = require("express")
const router = new Router()

const Reservation = require('../model/Reservation')

// 请求预约挂号信息数据 
router.post('/reservation/chioce', async (req, res) => {
    const { status, time1, time2, department, input } = req.body

    const isAll = status === '全部'
    const isTime = time1 === 0 && time2 === 0
    const isDepartment = department === undefined
    const isInput = input.length === 0

    // 全部
    if (isAll && isTime && isDepartment && isInput) {
        console.log("请求全部数据...");
        data = await Reservation.find()
    }
    // 只根据时间筛选
    else if (!isTime && isAll && isDepartment && isInput) {
        console.log("只根据时间筛选...");
        data = await Reservation.find(
            {
                bookingDate: {
                    "$gte": time1,
                    "$lte": time2
                }
            })
    }
    // 只根据单选框选择的值筛选
    else if (!isAll && isTime && isDepartment && isInput) {
        console.log("只根据单选框选择的值筛选...");
        data = await Reservation.find({ status: status })
    }
    // 只根据层级选择器选择的值筛选
    else if (!isDepartment && isAll && isTime && isInput) {
        console.log("只根据层级选择器选择的值筛选...");
        data = await Reservation.find({ department: department })
    }
    // 只根据搜索框输入值筛选
    else if (!isInput && isAll && isTime && isDepartment) {
        console.log("只根据搜索框输入值筛选...");
        data = await Reservation.find({
            $or: [
                { name: { $regex: input } },
                { doctor: { $regex: input } }
            ]
        })
    }
    // 单选框+时间
    else if (!isAll && !isTime && isDepartment && isInput) {
        console.log("单选框+时间...");
        data = await Reservation.find({
            status: status,
            bookingDate: {
                "$gte": time1,
                "$lte": time2
            }
        })
    }
    // 单选框+层级
    else if (!isAll && isTime && !isDepartment && isInput) {
        console.log("单选框+层级...");
        data = await Reservation.find({
            status: status,
            department: department
        })
    }
    // 时间+层级
    else if (isAll && !isTime && !isDepartment && isInput) {
        console.log("时间+层级...");
        data = await Reservation.find({
            bookingDate: {
                "$gte": time1,
                "$lte": time2
            },
            department: department
        })
    }
    // 单选框+时间+层级
    else if (!isAll && !isTime && !isDepartment && isInput) {
        console.log("单选框+时间+层级...");
        data = await Reservation.find({
            status: status,
            bookingDate: {
                "$gte": time1,
                "$lte": time2
            },
            department: department
        })
    }
    // 单选框+输入框
    else if (!isAll && isTime && isDepartment && !isInput) {
        console.log("单选框+输入框...");
        data = await Reservation.find({
            status: status,
            $or: [
                { name: { $regex: input } },
                { doctor: { $regex: input } }
            ]
        })
    }
    // 层级+输入框
    else if (isAll && isTime && !isDepartment && !isInput) {
        console.log("层级+输入框...");
        data = await Reservation.find({
            department: department,
            $or: [
                { name: { $regex: input } },
                { doctor: { $regex: input } }
            ]
        })
    }
    // 时间+输入框
    else if (!isAll && !isTime && isDepartment && !isInput) {
        console.log("时间+输入框...");
        data = await Reservation.find({
            bookingDate: {
                "$gte": time1,
                "$lte": time2
            },
            $or: [
                { name: { $regex: input } },
                { doctor: { $regex: input } }
            ]
        })
    }
    // 单选框+时间+输入框
    else if (!isAll && !isTime && isDepartment && !isInput) {
        console.log("单选框+时间+输入框...");
        data = await Reservation.find({
            status: status,
            bookingDate: {
                "$gte": time1,
                "$lte": time2
            },
            $or: [
                { name: { $regex: input } },
                { doctor: { $regex: input } }
            ]
        })
    }
    // 单选框+层级+输入框
    else if (!isAll && isTime && !isDepartment && !isInput) {
        console.log("单选框+层级+输入框...");
        data = await Reservation.find({
            status: status,
            department: department,
            $or: [
                { name: { $regex: input } },
                { doctor: { $regex: input } }
            ]
        })
    }
    // 时间+层级+输入框
    else if (isAll && !isTime && !isDepartment && !isInput) {
        console.log("时间+层级+输入框...");
        data = await Reservation.find({
            bookingDate: {
                "$gte": time1,
                "$lte": time2
            },
            department: department,
            $or: [
                { name: { $regex: input } },
                { doctor: { $regex: input } }
            ]
        })
    }
    // 单选框+时间+层级+输入框
    else if (!isAll && !isTime && !isDepartment && !isInput) {
        console.log("单选框+时间+层级...");
        data = await Reservation.find({
            status: status,
            bookingDate: {
                "$gte": time1,
                "$lte": time2
            },
            department: department,
            $or: [
                { name: { $regex: input } },
                { doctor: { $regex: input } }
            ]
        })
    }

    res.json({ status: 200, message: "数据请求成功", data })
})

// 删除数据（一条）
router.post('/reservation/deleteone', async (req, res) => {
    const { id } = req.body
    await Reservation.findByIdAndDelete(id)
    res.json({ status: 200, message: "数据删除成功" })
})

// 删除数据（多条）
router.post('/reservation/deletemany', async (req, res) => {
    const { id } = req.body
    await Reservation.deleteMany({ _id: { $in: id } })
    res.json({ status: 200, message: "数据删除成功" })
})

// 更改挂号备注
router.post('/reservation/tips', async (req, res) => {
    const { input, id } = req.body
    await Reservation.findOneAndUpdate({ _id: id }, { tips: input })
    res.json({ status: 200, message: "保存成功" })
})




module.exports = router